package com.wandian.cqcsk.mvvm.mine;

import android.app.Activity;
import android.app.Application;
import android.util.Log;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.BaseBean;
import com.mylib.libcore.bean.EveryDayConfigBean;
import com.mylib.libcore.bean.UserInfoBean;
import com.mylib.libcore.config.CoinTypeEnum;
import com.mylib.libcore.dialog.CommonGoldDialog;
import com.mylib.libcore.mvvm.BaseViewModel;
import com.mylib.libcore.network.rx.RxResultHelper;
import com.mylib.libcore.utils.JsonUtil;
import com.mylib.libcore.utils.RetrofitUtil;
import com.mylib.libcore.utils.UIUtils;
import com.mylib.libcore.utils.crc.AESUtils;
import com.wandian.cqcsk.bean.SignDataBean;
import com.wandian.cqcsk.data.BaseAppApi;
import com.wandian.setting.bean.CZDataBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

import static com.mylib.libcore.config.NetConfig.BASE_SUCCESS_CODE;


/**
 * @author Administrator
 * @date 2019/12/16-13:32
 * @description FragmentMineViewModel
 **/
public class FragmentMineViewModel extends BaseViewModel {
    private BaseAppApi baseAppApi;
    private CommonGoldDialog commonGoldDialog;//金币奖励弹框
    public MutableLiveData<UserInfoBean> userBean = new MutableLiveData<>();
    public MutableLiveData<String> signState = new MutableLiveData<>();
    public MutableLiveData<EveryDayConfigBean> everyDayConfig = new MutableLiveData<>();
    public FragmentMineViewModel(@NonNull Application application) {
        super(application);
        baseAppApi = RetrofitUtil.getAppRetrofit().create(BaseAppApi.class);
    }

    public void getUserInfo(){

        JsonObject jsonObject = new JsonObject();
        HashMap<String, String> hashMap = new HashMap<>();
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.getUserInfo(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {

                        Log.d("getUserInfo",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            try {
                                if(!UIUtils.isEmpty(bean.getData())){
                                    Log.d("crc_rc4key_getVersion", AESUtils.getInstance().decrypt(bean.getData()));
                                    UserInfoBean userInfoBean = JsonUtil.parseJsonToBean(AESUtils.getInstance().decrypt(bean.getData()),UserInfoBean.class);
                                    if(!UIUtils.isEmpty(userInfoBean)){
                                        userBean.setValue(userInfoBean);
                                        AppContext.getInstance().getAppPreferencesHelper().setUserInfoBean(userInfoBean);
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    //获取配置信息
    public void getConfigInfo(){
        JsonObject jsonObject = new JsonObject();
        HashMap<String, String> hashMap = new HashMap<>();
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.getEveryDayConfig(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        Log.d("getEveryDayConfig",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            try {
                                if(!UIUtils.isEmpty(bean.getData())){
                                    Log.d("crc_rc4key_getVersion", AESUtils.getInstance().decrypt(bean.getData()));
                                    EveryDayConfigBean everyDayConfigBean = JsonUtil.parseJsonToBean(AESUtils.getInstance().decrypt(bean.getData()),EveryDayConfigBean.class);
                                    if(!UIUtils.isEmpty(everyDayConfigBean)){
                                        everyDayConfig.setValue(everyDayConfigBean);
                                        AppContext.getInstance().getAppPreferencesHelper().setEveryDayConfig(everyDayConfigBean);
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    //签到
    public void todaySign(UserInfoBean userInfoBean){
        showDialog();
        JsonObject jsonObject = new JsonObject();
        HashMap<String, String> hashMap = new HashMap<>();
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.todaySign(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        dismissDialog();
                        Log.d("todaySign",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            userInfoBean.setIsSign(1);
                            AppContext.getInstance().getAppPreferencesHelper().setUserInfoBean(userInfoBean);
                            signState.setValue("1");
                            UIUtils.setKeyEveryDayCoin(CoinTypeEnum.SIGN_COIN);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    //获取任务奖励
    public void getTaskCoin(Activity activity, View view){
        showDialog();
        JsonObject jsonObject = new JsonObject();
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("advertName", "任务奖励");
        hashMap.put("sourceType", "task");
        hashMap.put("remark", "");
        jsonObject.addProperty("wdk", AESUtils.getInstance().encrypt(hashMap));
        RxResultHelper.getHttpObservable(getLifecycleProvider(), baseAppApi.taskMoney(jsonObject))
                .subscribe(new Observer<BaseBean<Object>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }
                    @Override
                    public void onNext(BaseBean<Object> bean) {
                        dismissDialog();
                        Log.d("taskMoney",   JsonUtil.toJson(bean));
                        if(!UIUtils.isEmpty(bean) && bean.getCode()==BASE_SUCCESS_CODE){
                            showGoldDialog(activity,view);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        dismissDialog();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void showGoldDialog(Activity activity, View view){
        try {
            int sys_width =  AppContext.getInstance().getAppPreferenceForever().getAppSysWidth()-70;
            if(sys_width<=0)sys_width = 375;
            commonGoldDialog = new CommonGoldDialog(sys_width,"","1",activity);
            commonGoldDialog.showAtLocation(view, Gravity.CENTER, 0, 0);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
